package com.prs.crm.action.person;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.person.Degree;
import com.prs.crm.domain.person.Weave;
import com.prs.crm.service.person.DegreeService;

public class DegreeAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private Degree degree;
	private DegreeService degreeService;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Degree getDegree() {
		return degree;
	}
	public void setDegree(Degree degree) {
		this.degree = degree;
	}
	public DegreeService getDegreeService() {
		return degreeService;
	}
	public void setDegreeService(DegreeService degreeService) {
		this.degreeService = degreeService;
	}
	
	@Action("listDegree")
	public String listDegree(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Degree.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDegreeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editDegree")
	public String editDegree(){
		if(ids!=null){
			this.degree=this.getDegreeService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveDegreeDeafult",results={
			@Result(name="success",type="redirect",location="listDegree"),
			@Result(name="error",type="chain",location="listDegree")
	})
	public String saveDegreeDeafult(){
		if(ids!=null){
			this.degree=this.getDegreeService().get(ids[0]);
		}
		degree.setType(1);
		this.getDegreeService().submit(degree);
		return SUCCESS;
	}
	
	@Action(value="saveDelDegreeDeafult",results={
			@Result(name="success",type="redirect",location="listDegree"),
			@Result(name="error",type="chain",location="listDegree")
	})
	public String saveDelDegreeDeafult(){
		List<Degree> list=this.getDegreeService().getAllDegree();
		for (int i = 0; i < list.size(); i++) {
			Degree job=list.get(i);
			if(job.getRemark()!=null){
				job.setType(2);
				this.getDegreeService().save(job);
			}
		}
		return SUCCESS;
	}
	
	@Action(value="saveDegree",results={
			@Result(name="success",type="redirect",location="listDegree"),
			@Result(name="error",type="chain",location="editDegree")
	})
	public String saveDegree(){
		degree.setType(2);
		this.getDegreeService().save(degree);
		return SUCCESS;
	}
	
	@Action(value="removeDegree",results={
			@Result(name="success",type="redirect",location="listDegree"),
			@Result(name="error",type="chain",location="listDegree")
	})
	public String removeDegree(){
		if (ids==null) {
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getDegreeService().batchRemove(ids);
		return SUCCESS;
	}
}
